// =============================================================
// CStatus class
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Jun. 18, 2007
//----------------------------------------------------------------------
#include "CStatus.h"
//----------------------------------------------------------------------
CStatus::CStatus()
{
	adjust();

	// initialise statgrab
	sg_init();
}
//----------------------------------------------------------------------
CStatus::~CStatus()
{
	adjust();

	sg_shutdown();
}
//----------------------------------------------------------------------
void CStatus::adjust()
{
	adjusted_nan = ADJUSTED_NAN;
	adjusted_zero = ADJUSTED_ZERO;
}
//----------------------------------------------------------------------
float CStatus::getLoad()
{
	if(sg_drop_privileges()!=0) {
		perror("Error : failed to drop privileges");
		return -1;
	}

	sg_load_stats *load_stat;
	if((load_stat=sg_get_load_stats())!=NULL) {
		return load_stat->min1;
		//return load_stat->min5;
		//return load_stat->min15;
	}

	return -1;
}
//----------------------------------------------------------------------
float CStatus::getCPU()
{	
	if(sg_drop_privileges()!=0) {
		perror("Error : failed to drop privileges");
		return -1; 
	}

	sg_cpu_percents *cpu_percent;
	if((cpu_percent = sg_get_cpu_percents())!=NULL) {
		float xxx = cpu_percent->idle;
		if(isnan(xxx)) return adjusted_nan;
		else if(xxx==0) return adjusted_zero;
		else {
			adjusted_nan = xxx;
			adjusted_zero = xxx;

			return xxx;
		}
		//return cpu_percent->iowait;
		//return cpu_percent->kernel;
		//return cpu_percent->user;
		//return cpu_percent->swap;
		//return cpu_percent->nice;
	}
	
	return -1;
}
//----------------------------------------------------------------------
